home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_11 / phillip2 / mainseg.c < prev    next >
C/C++ Source or Header  |  1993-06-26  |  10KB  |  330 lines

  1.  
  2.     /***********************************************
  3.     *
  4.     *   file d:\cips\mainseg.c
  5.     *
  6.     *   Functions: This file contains
  7.     *      main
  8.     *
  9.     *   Purpose:
  10.     *      This file contains the main calling
  11.     *      routine in a segmentation and related
  12.     *      operations program.
  13.     *
  14.     *   External Calls:
  15.     *      gin.c - get_image_name
  16.     *      numcvrt.c - get_integer
  17.     *                  int_convert
  18.     *      tiff.c - read_tiff_header
  19.     *               enhance_edges
  20.     *      hist.c - zero_histogram
  21.     *               smooth_histogram
  22.     *               show_histogram
  23.     *               calculate_histogram
  24.     *      segment.c - threshold_image_array
  25.     *                  grow
  26.     *                  find_peaks
  27.     *                  peaks_high_low
  28.     *                  valley_high_low
  29.     *                  threshold_and_find_means
  30.     *
  31.     *   Modifications:
  32.     *      27 September 1992 - created
  33.     *
  34.     *************************************************/
  35.  
  36. #include "cips.h"
  37.  
  38.  
  39.  
  40. short the_image[ROWS][COLS];
  41. short out_image[ROWS][COLS];
  42. unsigned long histogram[GRAY_LEVELS+1];
  43.  
  44. main(argc, argv)
  45.    int argc;
  46.    char *argv[];
  47. {
  48.    char   name[80], name2[80], response[80];
  49.    int    count, i, ie, il, j, k, le, length, ll,
  50.           peak1, peak2, size,
  51.           t, type, v, width;
  52.    short  background, hi, low, object, value;
  53.    struct tiff_header_struct image_header;
  54.  
  55.    my_clear_text_screen();
  56.  
  57.    if(argc < 7){
  58.     printf(
  59.      "\n\nmainseg in-file out-file hi low "
  60.      "value operation"
  61.      "\n\t\toperation = threshold grow peaks valleys "
  62.      "adaptive");
  63.     printf("\n");
  64.     exit(0);
  65.    }
  66.  
  67.    strcpy(name, argv[1]);
  68.    strcpy(name2, argv[2]);
  69.    hi    = atoi(argv[3]);
  70.    low   = atoi(argv[4]);
  71.    value = atoi(argv[5]);
  72.  
  73.    il = 1;
  74.    ie = 1;
  75.    ll = ROWS+1;
  76.    le = COLS+1;
  77.  
  78.    read_tiff_header(name, &image_header);
  79.  
  80.    length = (ROWS-10 + image_header.image_length)/ROWS;
  81.    width  = (COLS-10 +image_header.image_width)/COLS;
  82.    count  = 1;
  83.    printf("\nlength=%d  width=%d", length, width);
  84.  
  85.    create_file_if_needed(name, name2, out_image);
  86.    zero_histogram(histogram);
  87.  
  88.  
  89.       /*********************************
  90.       *
  91.       *   Manual Threshold operation
  92.       *
  93.       *********************************/
  94.  
  95.   if(argv[6][0] == 't'){
  96.      for(i=0; i<length; i++){
  97.         for(j=0; j<width; j++){
  98.           printf("\nrunning %d of %d", 
  99.                  count, length*width);
  100.           count++;
  101.              read_tiff_image(name, the_image,
  102.                   il+i*ROWS, ie+j*COLS,
  103.                   ll+i*ROWS, le+j*COLS);
  104.              printf("\nMS> Calling threshold");
  105.              threshold_image_array(the_image, 
  106.                         out_image, hi, low, value);
  107.              write_array_into_tiff_image(name2, 
  108.                               out_image,
  109.                               il+i*ROWS,
  110.                               ie+j*COLS,
  111.                               ll+i*ROWS,
  112.                               le+j*COLS);
  113.         }  /* ends loop over i */
  114.      }  /* ends loop over j */
  115.   }  /* ends if t */
  116.  
  117.  
  118.       /*********************************
  119.       *
  120.       *   Grow region operation
  121.       *
  122.       *********************************/
  123.  
  124.   if(argv[6][0] == 'g'){
  125.      for(i=0; i<length; i++){
  126.         for(j=0; j<width; j++){
  127.           printf("\nrunning %d of %d", 
  128.                  count, length*width);
  129.           count++;
  130.              read_tiff_image(name, the_image,
  131.                   il+i*ROWS, ie+j*COLS,
  132.                   ll+i*ROWS, le+j*COLS);
  133.              printf("\nMS> Calling grow");
  134.              grow(the_image, value);
  135.              write_array_into_tiff_image(name2, 
  136.                                     the_image,
  137.                                     il+i*ROWS,
  138.                                     ie+j*COLS,
  139.                                     ll+i*ROWS,
  140.                                     le+j*COLS);
  141.         }  /* ends loop over i */
  142.      }  /* ends loop over j */
  143.     }  /* ends if g */
  144.  
  145.  
  146.       /*********************************
  147.       *
  148.       *   Peak threshold operation
  149.       *
  150.       *********************************/
  151.  
  152.    if(argv[6][0] == 'p'){
  153.  
  154.        /* calculate histogram for the
  155.           entire image file */
  156.  
  157.       zero_histogram(histogram);
  158.       for(i=0; i<length; i++){
  159.          for(j=0; j<width; j++){
  160.             printf("\nrunning %d of %d", 
  161.                    count, length*width);
  162.             count++;
  163.             read_tiff_image(name, the_image,
  164.                     il+i*ROWS, ie+j*COLS,
  165.                     ll+i*ROWS, le+j*COLS);
  166.             printf("\nMS> Calling hist functions");
  167.             calculate_histogram(the_image, histogram);
  168.          }  /* ends loop over i */
  169.       }  /* ends loop over j */
  170.  
  171.       smooth_histogram(histogram);
  172.       show_histogram(histogram);
  173.       find_peaks(histogram, &peak1, &peak2);
  174.       printf("\npeak1=%d  peak2=%d", peak1, peak2);
  175.       peaks_high_low(histogram, peak1, peak2,
  176.                      &hi, &low);
  177.       printf("\nhi=%d low=%d", hi, low);
  178.  
  179.               /* now read the image file again
  180.                  and threshold and grow objects. */
  181.       count = 1;
  182.       for(i=0; i<length; i++){
  183.          for(j=0; j<width; j++){
  184.             printf("\nrunning %d of %d", 
  185.                    count, length*width);
  186.             count++;
  187.             read_tiff_image(name, the_image,
  188.                    il+i*ROWS, ie+j*COLS,
  189.                    ll+i*ROWS, le+j*COLS);
  190.             threshold_image_array(the_image, out_image,
  191.                                   hi, low, value);
  192.             write_array_into_tiff_image(name2, 
  193.                                out_image,
  194.                                il+i*ROWS,
  195.                                ie+j*COLS,
  196.                                ll+i*ROWS,
  197.                                le+j*COLS);
  198.          }  /* ends loop over i */
  199.       }  /* ends loop over j */
  200.    }  /* ends if p */
  201.  
  202.  
  203.  
  204.       /*********************************
  205.       *
  206.       *   Valley threshold operation
  207.       *
  208.       *********************************/
  209.  
  210.    if(argv[6][0] == 'v'){
  211.  
  212.        /* calculate histogram for the
  213.           entire image file */
  214.  
  215.       zero_histogram(histogram);
  216.       for(i=0; i<length; i++){
  217.          for(j=0; j<width; j++){
  218.             printf("\nrunning %d of %d", 
  219.                    count, length*width);
  220.             count++;
  221.             read_tiff_image(name, the_image,
  222.                     il+i*ROWS, ie+j*COLS,
  223.                     ll+i*ROWS, le+j*COLS);
  224.             printf("\nMS> Calling hist functions");
  225.             calculate_histogram(the_image, histogram);
  226.          }  /* ends loop over i */
  227.       }  /* ends loop over j */
  228.  
  229.  
  230.       smooth_histogram(histogram);
  231.       show_histogram(histogram);
  232.       find_peaks(histogram, &peak1, &peak2);
  233.       printf("\npeak1=%d  peak2=%d", peak1, peak2);
  234.       valley_high_low(histogram, peak1, peak2,
  235.                       &hi, &low);
  236.       printf("\nhi=%d low=%d", hi, low);
  237.  
  238.               /* now read the image file again
  239.                  and threshold and grow objects. */
  240.       count = 1;
  241.       for(i=0; i<length; i++){
  242.          for(j=0; j<width; j++){
  243.             printf("\nrunning %d of %d", 
  244.                    count, length*width);
  245.             count++;
  246.             read_tiff_image(name, the_image,
  247.                    il+i*ROWS, ie+j*COLS,
  248.                    ll+i*ROWS, le+j*COLS);
  249.             threshold_image_array(the_image, out_image,
  250.                                   hi, low, value);
  251.             write_array_into_tiff_image(name2, 
  252.                                out_image,
  253.                                il+i*ROWS,
  254.                                ie+j*COLS,
  255.                                ll+i*ROWS,
  256.                                le+j*COLS);
  257.          }  /* ends loop over i */
  258.       }  /* ends loop over j */
  259.    }  /* ends if v */
  260.  
  261.       /*********************************
  262.       *
  263.       *   Adaptive threshold operation
  264.       *
  265.       *********************************/
  266.  
  267.    if(argv[6